package com.fenger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fenger.contast.SysConstast;
import com.fenger.domain.Topic;
import com.fenger.mapper.TopicMapper;
import com.fenger.service.TopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fenger.utils.DataGirdView;
import com.fenger.vo.TopicVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 方
 * @since 2019-04-29
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    TopicMapper topicMapper;
    @Override
    public DataGirdView queryAllTopicForTable(TopicVo topicVo) {
//        for (int i = 0; i < 50; i++) {
//            Topic entity = new Topic();
//            entity.setAuthor("make"+i);
//            entity.setClickamount(i);
//            entity.setContent("jkdsfalkdf"+i);
//            entity.setTitle("lik"+i);
//            entity.setCreatedate(new Date());
//            topicMapper.insert(entity);
//
//        }
        Page<Object> objects = PageHelper.startPage(topicVo.getPage(), topicVo.getLimit());
        List<Topic> topics = topicMapper.selectList(null);
        return new DataGirdView(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,objects.getTotal(),topics);
    }

    @Override
    public DataGirdView insertTopic(TopicVo topicVo) {
        try {
            topicVo.setClickamount(0);
            topicVo.setCreatedate(new Date());
            topicMapper.insert(topicVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE,SysConstast.ADD_SUCCESS_MSG,0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE,SysConstast.ADD_ERROR_MSG,0L,null);
        }
    }

    @Override
    public DataGirdView updateTopic(TopicVo topicVo) {
        try {
            topicMapper.updateById(topicVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE,SysConstast.UPDATE_SUCCESS_MSG,0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE,SysConstast.UPDATE_ERROR_MSG,0L,null);
        }
    }
    @Override
    public DataGirdView addClickamount(TopicVo topicVo) {
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("topicid",topicVo.getTopicid());
            Topic topic = topicMapper.selectOne(wrapper);
            topic.setClickamount(topic.getClickamount()+1);
            topicMapper.updateById(topic);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE,SysConstast.UPDATE_SUCCESS_MSG,0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE,SysConstast.UPDATE_ERROR_MSG,0L,null);
        }
    }

    @Override
    public Topic queryTopicByTopicid(TopicVo topicVo) {
        Topic topic = this.topicMapper.selectById(topicVo.getTopicid());
        return topic;
    }

    @Override
    public DataGirdView deleteTopic(TopicVo topicVo) {
        try {
            topicMapper.deleteById(topicVo.getTopicid());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE,SysConstast.DELETE_SUCCESS_MSG,0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE,SysConstast.DELETE_ERROR_MSG,0L,null);
        }
    }
}
